/**
 * 
 */
package com.icbc.cfsmgmt.common.productfile;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.icbc.cc.api.ApiCommonUtils;
import com.icbc.cc.api.CallApi;
import com.icbc.cc.security.app.CommonHttpRequest;
import com.icbc.cte.base.CTEConstance;
import com.icbc.cte.base.TranFailException;
import com.icbc.cte.logging.Log;
import com.icbc.cte.logging.LogFactory;
import com.icbc.ctp.core.IContext;
import com.icbc.ctp.core.annotations.Input;
import com.icbc.ctp.core.annotations.Output;
import com.icbc.ctp.core.impl.OperationStep;

/**
 * TODO 类的描述：。 
 * <pre>
 *
 * </pre>
 *
 * <pre>
 * modify by lenovo on 2016-3-23
 *    fix->1.
 *         2.
 * </pre> 
 */
@Input(name = {"condition"}, type = {"java:java.lang.String"}, mustInput = {true}, isVariable = {true}, isArray = {false}, description = {"查询条件"})
@Output(name = { "itemList"}, type = { "java:java.util.List"}, isArray = { false}, description = {
		"产品列表"})
public class GetProductList extends OperationStep {
	private static Log log = LogFactory.getCtpLog(CallApi.class);
	
	//产品管理系统接口地址
	private static String API_ROOT = CTEConstance.getEnvProperty("apiRoot");
	//---------------------------产品---------------------------
	private static String GET_PRODUCT_List = "/ApiServlet?opName=query_prolist_fuzzy"; // 获取产品列表
	
	private static String CALL_API_REQ_METHOD = "POST";
	private static String CALL_API_CHARSET = "UTF-8";
	private static int CALL_API_TIME_OUT = 300000;
	
	/* (non-Javadoc)
	 * @see com.icbc.ctp.core.impl.OperationStep#execute(com.icbc.ctp.core.IContext)
	 */
	@Override
	protected int execute(IContext context) throws TranFailException {
		// TODO Auto-generated method stub
		try{

			//2 转化为json格式
			JSONObject paramJson = new JSONObject();
			//3 将一些默认数据封装到json中
			paramJson.put("condition", (String)getInputValue(context,"condition"));
			//4 加密
			String newparam = ApiCommonUtils.encrypAndSign(paramJson.toJSONString(), "3");
			
			
			String remsg = null;
			try {
				remsg = CommonHttpRequest.doHttpRequest(API_ROOT + GET_PRODUCT_List+"&osFlag="+3,
						CALL_API_REQ_METHOD, CALL_API_TIME_OUT,
						"param=" + newparam, CALL_API_CHARSET);
			} catch (Exception e) {
				e.printStackTrace();
			}
			//解密
			String str = ApiCommonUtils.decrypAndSign(remsg, ApiCommonUtils.APP_CALL_FLAG, "3");
			JSONObject objResult = (JSONObject) JSONObject.parse(str);
			String retCode = objResult.getString("retCode");
			
			
			
			JSONObject objParam = new JSONObject();

			if ("0".equals(retCode)) {
			
					if(objResult.get("prodList") !=null && !"".equals(objResult.get("prodList").toString())){
						//产品列表
						JSONArray arrRespData = (JSONArray)objResult.get("prodList"); 

						List<JSONObject> newitemList=new ArrayList<JSONObject>();
						for(int i=0;i<arrRespData.size();i++){
							JSONObject json=(JSONObject)arrRespData.get(i);
							JSONObject newjson=new JSONObject();
							//产品id
							newjson.put("prodId", json.get("id"));
							//产品名称
							newjson.put("prodName", json.get("proName"));
							//产品编码
							newjson.put("proCode", json.get("proCode"));
							
							newitemList.add(newjson);
						}
						//objParam.put("productList", newitemList);
						context.setValueAt("itemList", newitemList);
					}else{
						objParam.put("itemList", "");
					}
					
					
				
					objParam.put("retCode", "0");
					objParam.put("retMsg", "产品列表获取成功");
			}  else {

				objParam.put("retCode", "-1");
				objParam.put("retMsg", "处理失败，请联系客服");
			}		
			
//			context.setValueAt("fileUrl", fileUrl);
//			context.setValueAt("oriFileName", fileName + "." + fileExt);
//			context.setValueAt("relFileName", fileNameWithOutExt + "." + fileExt);
			context.setValueAt("retCode", 0);
			
		}catch(Exception e){
			System.out.println(e);
			context.setValueAt("retCode", -1);
		}
		return 0;
	}

}
